<template>
  <div class="root">
    <div class="head" style="display: flex">
      <div class="searchItem">
        <span>名称:&nbsp;</span>
        <el-input
          style="width: 200px"
          v-model="query.name"
          autocomplete="off"
          placeholder="请输入姓名"
        />
      </div>
      <div class="searchItem">
        <el-button type="primary" @click="onAddItem"> 新增 </el-button>
      </div>
    </div>
    <el-table
      :header-cell-style="{ background: '#f5f7fa', color: '#333' }"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column prop="code" label="编号" />
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="time" label="借书事件" />
      <el-table-column prop="status" label="使用状态" />
      <el-table-column prop="sum" label="借书次数" />
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-button
            link
            type="primary"
            size="small"
            @click.prevent="deleteRow(scope.$index)"
          >
            移除
          </el-button>
          <el-button
            link
            type="primary"
            size="small"
            @click.prevent="editRow(scope.row)"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 20px; display: flex; justify-content: flex-end">
      <el-pagination background layout="prev, pager, next" :total="88" />
    </div>

    <el-dialog :title="dialogTittle" v-model="dialogVisible">
      <el-form
        v-if="dialogVisible"
        ref="ruleFormRef"
        :model="submitFormdata"
        status-icon
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
      >
        <el-form-item label="借书事件" prop="time">
          <el-date-picker
            v-model="submitFormdata.time"
            type="date"
            placeholder="选择时间"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="submitFormdata.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="使用状态" prop="status">
          <el-input v-model="submitFormdata.status" autocomplete="off" />
        </el-form-item>
        <el-form-item label="借书次数" prop="sum">
          <el-input-number
            input
            v-model="submitFormdata.sum"
            autocomplete="off"
            :min="1"
          />
        </el-form-item>

        <el-form-item>
          <div style="display: flex; justify-content: flex-end; width: 100%">
            <el-button @click="resetForm()">取消</el-button>
            <el-button type="primary" @click="submitForm()"> 提交 </el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { userLIst } from "@/api/home/index";
import { ElMessage } from "element-plus";
let list1 = reactive([
  {
    key: 1,
    name: "忍村",
  },
  {
    key: 2,
    name: "火村",
  },
  {
    key: 3,
    name: "土村",
  },
]);

const tableData = ref([]);
const deleteRow = (index) => {
  tableData.value.splice(index, 1);
};
let dialogTittle = ref("");
let dialogVisible = ref(false);
let editRow = (e) => {
  dialogTittle.value = "编辑";
  submitFormdata.value = JSON.parse(JSON.stringify(e));
  dialogVisible.value = true;
};
const onAddItem = () => {
  dialogTittle.value = "新增";
  submitFormdata.value = {};
  dialogVisible.value = true;
};

let ruleFormRef = ref(null);
const rules = reactive({
  time: [{ required: true, message: "请填写时间" }],
  name: [{ required: true, message: "请填写名称" }],
});
let submitFormdata = ref({});
const submitForm = () => {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      console.log("submit!");
      if (dialogTittle.value == "编辑") {
        tableData.value.forEach((item, index) => {
          if (item.code == submitFormdata.value.code) {
            tableData.value[index] = { ...item, ...submitFormdata.value };
          }
        });
        dialogVisible.value = false;
        console.log(tableData.value);
        ElMessage.success("操作成功");
      }
      if (dialogTittle.value == "新增") {
        submitFormdata.value.code = Math.floor(Math.random() * 9000) + 1000;
        dialogVisible.value = false;
        tableData.value.push(submitFormdata.value);
        ElMessage.success("操作成功");
        submitFormdata.value = {};
      }
    } else {
      ElMessage.error("请完成表单");
    }
  });
};

const resetForm = () => {
  submitFormdata.value = {};
  dialogVisible.value = false;
};
let query = ref({ name: "" });

onMounted(() => {
  tableData.value = [
    {
      code: "0001",
      time: "2016-05-01",
      name: "羽毛球",
      status: "已使用",
      sum: 3,
      kg: "20g",
    },
    {
      code: "0002",

      time: "2016-05-01",
      name: "足球",
      status: "已使用",
      sum: 3,
      kg: "20g",
    },
    {
      code: "0003",

      time: "2016-05-01",
      name: "篮球",
      status: "已使用",
      sum: 3,
      kg: "20g",
    },
  ];
});
</script>

<style lang="scss" scoped>
.root {
  width: 100%;
}
.head {
  margin-bottom: 20px;
  .searchItem {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}
// /deep/ .el-select__selected-item .el-select__placeholder{
//     color: pink !important;
// }
// ::v-deep .el-select__wrapper .is-focused{
//     box-shadow: 0 0 0 1px pink !important;
// }
</style>
